package com.tengfei.blogsystem.controller.admin;

/**
 * @version v1.0
 * @author} feige
 * @date 2021-10-15-16:22
 * @description
 */

import com.tengfei.blogsystem.pojo.Article;
import com.tengfei.blogsystem.response.ResponseResult;
import com.tengfei.blogsystem.service.IArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 文章api
 */
@PreAuthorize("@permission.admin()")    //需要管理员权限
@RestController
@RequestMapping("/admin/article")
public class ArticleAdminApi {

    @Autowired
    private IArticleService articleService;

    /**
     * 获取文章列表
     *
     * @param page       页码
     * @param size       每页数量
     * @param state      正文/草稿
     * @param keywords   关键字
     * @param categoryId 分类id
     * @return
     */
    @GetMapping("/list/{page}/{size}")
    public ResponseResult listArticle(@PathVariable("page") int page, @PathVariable("size") int size,
                                      @RequestParam(value = "state", required = false) String state,
                                      @RequestParam(value = "keywords", required = false) String keywords,
                                      @RequestParam(value = "categoryId", required = false) String categoryId) {
        return articleService.listArticle(page, size, state, keywords, categoryId);
    }

    /**
     * 添加文章
     *
     * @param article
     * @return
     */
    @PostMapping("/")
    public ResponseResult postArticle(@RequestBody Article article, HttpServletRequest request, HttpServletResponse response) {
        return articleService.postArticle(article, request, response);
    }

    /**
     * 删除文章
     *
     * @param articleId
     * @return
     */
    @DeleteMapping("/{articleId}")
    public ResponseResult deleteArticle(@PathVariable("articleId") String articleId) {

        return null;
    }

    /**
     * 修改文章
     *
     * @param articleId
     * @param article
     * @return
     */
    @PutMapping("/{articleId}")
    public ResponseResult updateArticle(@PathVariable("articleId") String articleId, @RequestBody Article article) {

        return null;
    }

    /**
     * 查找文章
     *
     * @param articleId
     * @return
     */
    @GetMapping("/{articleId}")
    public ResponseResult getArticle(@PathVariable("articleId") String articleId,
                                     HttpServletRequest request,
                                     HttpServletResponse response) {
        return articleService.getArticle(articleId,request,response);
    }

    /**
     * 更新状态
     *
     * @param articleId
     * @param state
     * @return
     */
    @PutMapping("/state/{articleId}/{state}")
    public ResponseResult updateArticleState(@PathVariable("state") String state, @PathVariable("articleId") String articleId) {

        return null;
    }

    /**
     * 文章置顶
     *
     * @param articleId
     * @return
     */
    @PutMapping("/top/{articleId}")
    public ResponseResult updateArticleState(@PathVariable("articleId") String articleId) {

        return null;
    }
}
